/*******************************************************************************
* This file provides the functions for the Timer
*
* Author: Kong Wai Weng @ Cytron Technologies Sdn. Bhd.
*******************************************************************************/



#include "hal_timer.h"



/*******************************************************************************
* PUBLIC FUNCTION: vInitTimer2
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initialize the Timer 2 (Time base for Output Compare module).
*
*******************************************************************************/
void vInitTimer2(void)
{
	// Disable Timer 2 if enabled previously.
	CloseTimer2();
	
	// Reset Timer 2.
	WriteTimer2(0);
	
	// Configure Timer 2 interrupt.
	ConfigIntTimer2(	T2_INT_OFF &		// Timer 2 interrupt disabled.
						T2_INT_PRIOR_0	);	// Interrupt priority 0.
	
	// Open Timer 2.
	OpenTimer2(	T2_ON &				// Starts the timer.
				T2_IDLE_CON &		// Continue timer operation in Idle mode.
				T2_GATE_OFF &		// Gated time accumulation disabled.
				T2_PS_1_8 &			// 1:8 prescale value.
				T2_SOURCE_INT,		// Internal clock (FOSC/2).
				49999	);			// Period = 100Hz.
}	



/*******************************************************************************
* PUBLIC FUNCTION: vInitTimer3
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initialize the Timer 3 (Time base for Input Capture module).
*
*******************************************************************************/
void vInitTimer3(void)
{
	// Disable Timer 3 if enabled previously.
	CloseTimer3();
	
	// Reset Timer 3.
	WriteTimer3(0);
	
	// Configure Timer 3 interrupt.
	ConfigIntTimer3(	T3_INT_OFF &		// Timer 3 interrupt disabled.
						T3_INT_PRIOR_0	);	// Interrupt priority 0.
	
	// Open Timer 3.
	OpenTimer3(	T3_ON &				// Starts the timer.
				T3_IDLE_CON &		// Continue timer operation in Idle mode.
				T3_GATE_OFF &		// Gated time accumulation disabled.
				T3_PS_1_8 &			// 1:8 prescale value.
				T3_SOURCE_INT,		// Internal clock (FOSC/2).
				0xffff	);			// Period = maximum value of 16-bit.
}	
